package com.wpw.java7demo.file;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.SynchronousQueue;

import org.junit.Test;

public class ParhTest {

	@Test
	public void test1() {
		//Path path = FileSystems.getDefault().getPath("","test.txt");
		Path path = Paths.get("C:\\", "path", "Soft.txt");
		System.out.printf("是否是绝对路径：%s\n", path.isAbsolute());
		System.out.printf("文件名称: %s\n", path.getFileName());
		System.out.printf("根目录: %s\n", path.getRoot());
		for (int index = 0; index < path.getNameCount(); index++) {
			System.out.printf("getName(%d): %s\n", index, path.getName(index));
		}
		System.out.printf("路径截取: %s\n", path.subpath(0, 2));
		System.out.printf("父目录: %s\n", path.getParent());
		System.out.println("是否绝对路径:" + path.isAbsolute());
		
		System.out.println("===========");
		
		path = Paths.get("", "test.txt");
		System.out.printf("是否是绝对路径：%s\n", path.isAbsolute());
		System.out.printf("文件是否存在：%s\n", path.toFile().exists());
		System.out.printf("绝对路径地址: %s\n", path.toAbsolutePath());
		System.out.printf("根目录: %s\n", path.getRoot());//null

		System.out.println("===========");
		
		 path = path.toAbsolutePath();
		 System.out.printf("是否是绝对路径：%s\n", path.isAbsolute());
		 System.out.printf("文件是否存在：%s\n", path.toFile().exists());
		 System.out.printf("绝对路径地址: %s\n", path.toAbsolutePath());
		 System.out.printf("根目录: %s\n", path.getRoot());//null

	}

	@Test
	public void test2() {
		Path path = Paths.get("D:\\", "path\\Soft.txt");
		File file = path.toFile();
		System.out.println(file.exists());
		
		Path path2 = file.toPath();
		System.out.println(path2);
		
		Path path3 = Paths.get("test.txt");
		System.out.println(path3.toAbsolutePath());
		
		
	}
	
	@Test
	public void test3() {
		
		Path root = Paths.get("D:\\");
		
		//参数是绝对路径
		Path jd = root.resolve("D:\\path\\Soft.txt");
		System.out.println(jd.toAbsolutePath());
		
		//参数是相对路径，结果是相对于root
		Path xd = root.resolve("path\\Soft.txt");
		System.out.println(xd.toAbsolutePath());
		
		Path firefox = xd.resolveSibling("Soft\\firefox");
		System.out.println(firefox.toAbsolutePath());
	}
	
	@Test
	public void test4() {
		Path relativize = Paths.get("/home/temp/a").relativize(Paths.get("/home/usr/b"));
		System.out.println(relativize);
		
	}
	
	@Test
	public void test5() {
		Path normalize = Paths.get("/home/temp/../usr/../apply/tomcat/webapp").normalize();
		System.out.println(normalize);
	}

}
